package com.woniuxy.project115.kaer.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project115.kaer.platform.entity.Role;
import com.woniuxy.project115.kaer.platform.mapper.RoleMapper;
import com.woniuxy.project115.kaer.platform.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fyy
 * @since 2025年02月23日
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public void addAdminRole(String roleName, String remarks) throws Exception {
        final QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name",roleName);
        if(roleMapper.selectOne(roleQueryWrapper)!=null){
            throw new Exception("已有该角色，请勿重复添加");
        }
        final Role role = new Role();
        role.setRoleName(roleName);
        role.setRemarks(remarks);
        roleMapper.insert(role);
    }

    @Override
    public List<Role> getAllRole() throws Exception {
        //查询所有角色，添加到list中，返回list
        final List<Role> roles = roleMapper.selectList(null);
        if(roles.size()==0){
            throw new Exception("没有角色，请先添加角色");
        }
        return roles;
    }

    @Override
    public void deleteRoleByName(Integer roleId) throws Exception {
        final QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id",roleId);
        final Role role = roleMapper.selectOne(roleQueryWrapper);
        if(role==null){
            throw new Exception("没有该角色，请检查角色名称");
        }
        roleMapper.deleteById(role.getId());
    }


}
